package lucene;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;

public class IndexSearcherDemo {
	public static void main(String[] args) throws CorruptIndexException, IOException {
		IndexReader r = IndexReader.open(FSDirectory.open(new File("../mydemos/resource/index/")), true);
		IndexSearcher s = new IndexSearcher(r);
		
		Term t = new Term("contents", "friend");
		Query query = new TermQuery(t);
//		Query query = new PrefixQuery(t);
		TopDocs d = s.search(query , 10);
		ScoreDoc[] ds = d.scoreDocs;
		for(ScoreDoc sd : ds) {
			System.out.println(sd.doc);
			System.out.println(s.explain(query, sd.doc));
		}
		
	}
}
